home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 December / december_2001.iso / Internet Programs / HTML Webmaster 2.0 / _SETUP.1 / counter.cod < prev    next >
Encoding:
Text File  |  2001-03-04  |  3.3 KB  |  79 lines

  1.  
  2. <!--- Individual Counter --->
  3. <SCRIPT LANGUAGE="JavaScript">
  4. <!-- Hide from old browsers
  5. // Copyright ⌐ 2000 Casper HTML
  6.  
  7. // Boolean variable specified if alert should be displayed if cookie exceeds 4KB
  8. var caution = false
  9.  
  10. // name - name of the cookie
  11. // value - value of the cookie
  12. // [expires] - expiration date of the cookie (defaults to end of current session)
  13. // [path] - path for which the cookie is valid (defaults to path of calling document)
  14. // [domain] - domain for which the cookie is valid (defaults to domain of calling document)
  15. // [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
  16. // * an argument defaults when it is assigned null as a placeholder
  17. // * a null placeholder is not required for trailing omitted arguments
  18. function setCookie(name, value, expires, path, domain, secure) {
  19.         var curCookie = name + "=" + escape(value) +
  20.                 ((expires) ? "; expires=" + expires.toGMTString() : "") +
  21.                 ((path) ? "; path=" + path : "") +
  22.                 ((domain) ? "; domain=" + domain : "") +
  23.                 ((secure) ? "; secure" : "")
  24.         if (!caution || (name + "=" + escape(value)).length <= 4000)
  25.                 document.cookie = curCookie
  26.         else
  27.                 if (confirm("Cookie exceeds 4KB and will be cut!"))
  28.                         document.cookie = curCookie
  29. }
  30.  
  31. // name - name of the desired cookie
  32. // * return string containing value of specified cookie or null if cookie does not exist
  33. function getCookie(name) {
  34.         var prefix = name + "="
  35.         var cookieStartIndex = document.cookie.indexOf(prefix)
  36.         if (cookieStartIndex == -1)
  37.                 return null
  38.         var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
  39.         if (cookieEndIndex == -1)
  40.                 cookieEndIndex = document.cookie.length
  41.         return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
  42. }
  43.  
  44. // name - name of the cookie
  45. // [path] - path of the cookie (must be same as path used to create cookie)
  46. // [domain] - domain of the cookie (must be same as domain used to create cookie)
  47. // * path and domain default if assigned null or omitted if no explicit argument proceeds
  48. function deleteCookie(name, path, domain) {
  49.         if (getCookie(name)) {
  50.                 document.cookie = name + "=" + 
  51.                 ((path) ? "; path=" + path : "") +
  52.                 ((domain) ? "; domain=" + domain : "") +
  53.                 "; expires=Thu, 01-Jan-70 00:00:01 GMT"
  54.         }
  55. }
  56.  
  57. // date - any instance of the Date object
  58. // * you should hand all instances of the Date object to this function for "repairs"
  59. // * this function is taken from Chapter 14, "Time and Date in JavaScript", in "Learn Advanced JavaScript Programming"
  60. function fixDate(date) {
  61.         var base = new Date(0)
  62.         var skew = base.getTime()
  63.         if (skew > 0)
  64.                 date.setTime(date.getTime() - skew)
  65. }
  66.  
  67. var now = new Date()
  68. fixDate(now)
  69. now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000)
  70. var visits = getCookie("counter")
  71. if (!visits)
  72.         visits = 1
  73. else
  74.         visits = parseInt(visits) + 1
  75. setCookie("counter", visits, now)
  76. document.write("You have been here " + visits + " time(s).")
  77.  
  78. // -->
  79. </SCRIPT>